//
//  LZLeadingView.m
//  laziz_Merchant
//
//  Created by xbn_iMac on 2017/1/12.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZLeadingView.h"
#import "UIScrollView+LZContentOffset.h"
#import "Masonry.h"

@interface LZLeadingView ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *scrollView;

@end

@implementation LZLeadingView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSArray *arrImages = [self subviewImageNameArray];
        NSArray *arrTextImages = [self subviewTextImageArray];
        
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
        scroll.backgroundColor = [UIColor whiteColor];
        scroll.showsHorizontalScrollIndicator = NO;
        scroll.showsVerticalScrollIndicator = NO;
        scroll.contentSize = CGSizeMake(CGRectGetWidth(frame)*arrImages.count, CGRectGetHeight(frame));
        scroll.delegate = self;
        scroll.pagingEnabled = YES;
        scroll.bounces = NO;
        [self addSubview:scroll];
        
        self.scrollView = scroll;
        
        UIView *container = [[UIView alloc] init];
        [scroll addSubview:container];
        
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(scroll);
            make.height.equalTo(self);
        }];
        
        UIView *lastView = nil;
        
        for (int i = 0; i<arrImages.count; i++) {
            
            /* ********************** 引导页的背景图 **********************  */
            UIImageView *subImgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*CGRectGetWidth(scroll.frame), 0,CGRectGetWidth(scroll.frame), CGRectGetHeight(scroll.frame))];
            subImgView.userInteractionEnabled = YES;
            subImgView.image = [UIImage imageNamed:arrImages[i]];
            [container addSubview:subImgView];
            
            [subImgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.and.bottom.mas_equalTo(0);
                make.width.mas_equalTo(CGRectGetWidth(scroll.frame));
                if (lastView) {
                    make.leading.equalTo(lastView.mas_trailing);
                }
                else {
                    make.leading.mas_equalTo(0);
                }
            }];
            
            lastView = subImgView;
            
            /* ********************** 引导页的文字图 **********************  */
            UIImageView *textImage = [[UIImageView alloc] init];
            textImage.image = [UIImage imageNamed:arrTextImages[i]];
            [subImgView addSubview:textImage];
            
            GET_SCREEN_SCALE(scale);
            
            CGFloat ratio = textImage.image.size.height/textImage.image.size.width;
            
            [textImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(64*scale);
                make.centerX.equalTo(subImgView.mas_centerX);
                make.width.mas_equalTo(SCREEN_WIDTH-53*scale);
                make.height.equalTo(textImage.mas_width).multipliedBy(ratio);
            }];
            
            if (i == arrImages.count-1) {
                /* ********************** 引导页的最后一页的按钮 **********************  */
                UIButton *btn = [[UIButton alloc] init];
                btn.layer.cornerRadius = 4;
                btn.layer.borderWidth = 0.5;
                btn.layer.borderColor = UIColorFromRGB(0x000000, 1).CGColor;
                [btn setTitle:LZLocalizedString(@"leading_button_start", nil) forState:UIControlStateNormal];
                [btn setTitleColor:UIColorFromRGB(0x000000, 1) forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:18.0];
                [btn addTarget:self action:@selector(enterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [subImgView addSubview:btn];
                
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo([btn intrinsicContentSize].width+52);
                    make.height.mas_equalTo(35);
                    make.centerX.equalTo(subImgView.mas_centerX);
                    make.bottom.mas_equalTo(-25);
                }];
            }
            else {
                /* ********************** 引导页的PageControl **********************  */
                UIPageControl *pageCtrl = [[UIPageControl alloc] init];  //创建UIPageControl，位置在屏幕最下方。
                pageCtrl.numberOfPages = arrImages.count;//总的图片页数
                pageCtrl.currentPage = i; //当前页
                pageCtrl.currentPageIndicatorTintColor = UIColorFromRGB(0xffffff, 1);  // 选中的点的颜色
                pageCtrl.pageIndicatorTintColor = UIColorFromRGB(0xffffff, 0.3);          // 未选中的点的颜色
                pageCtrl.userInteractionEnabled = NO;
                [subImgView addSubview:pageCtrl];  //将UIPageControl添加到主界面上。
                
                [pageCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(-25);
                    make.height.mas_equalTo(7.5);
                    make.centerX.equalTo(subImgView.mas_centerX);
                }];
            }
        }
        
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(lastView.mas_trailing);
        }];
        
    }
    return self;
}

-(NSArray*)subviewImageNameArray {
    if (IS_IPHONE_4) {
        return @[@"leading_iphone4_1",@"leading_iphone4_2",@"leading_iphone4_3"];
    }
    else if (IS_IPHONE_6P) {
        return @[@"leading_iphone6p_1",@"leading_iphone6p_2",@"leading_iphone6p_3"];
    }
    else {
        return @[@"leading_iphone6_1",@"leading_iphone6_2",@"leading_iphone6_3"];
    }
}

-(NSArray*)subviewTextImageArray {
    if (IS_IPHONE_4) {
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            return @[@"leading_iphone4_1_subimage_ar",@"leading_iphone4_2_subimage_ar",@"leading_iphone4_3_subimage_ar"];
        }
        else {
            return @[@"leading_iphone4_1_subimage_en",@"leading_iphone4_2_subimage_en",@"leading_iphone4_3_subimage_en"];
        }
    }
    else if (IS_IPHONE_6P) {
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            return @[@"leading_iphone6p_1_subimage_ar",@"leading_iphone6p_2_subimage_ar",@"leading_iphone6p_3_subimage_ar"];
        }
        else {
            return @[@"leading_iphone6p_1_subimage_en",@"leading_iphone6p_2_subimage_en",@"leading_iphone6p_3_subimage_en"];
        }
    }
    else {
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            return @[@"leading_iphone6_1_subimage_ar",@"leading_iphone6_2_subimage_ar",@"leading_iphone6_3_subimage_ar"];
        }
        else {
            return @[@"leading_iphone6_1_subimage_en",@"leading_iphone6_2_subimage_en",@"leading_iphone6_3_subimage_en"];
            
        }
    }
}

-(void)enterButtonAction:(UIButton*)sender
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        if (self.dismissLeadingViewBlock) {
            self.dismissLeadingViewBlock();
        }
        [self removeFromSuperview];
    }];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self.scrollView setLanguageContentOffset];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}

@end
